/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package service;

import map.file.CachedFileMap;
import figures.FigureIterator;
import file.FileGetter;
import geometry.Rectangle;

public class Service extends CachedFileMap implements GisService{

	int expireTime;
	long nextUpdate;
	boolean enabled = false;
	String name;
	
	public Service(FileGetter getter, String file, int expireTime, String name) {
		super(getter, file, null);
		this.expireTime=expireTime;
		this.name = name;
	}
	
	private boolean timeExpired(){
		return nextUpdate > System.currentTimeMillis();
	}
	
	private void resetTimer(){
		this.nextUpdate=System.currentTimeMillis() + this.expireTime*1000;
	}
	
	public void refresh(){
		if(enabled && timeExpired()){
			load();
			resetTimer();
		}
	}

	public String getName() {
		return name;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean b) {
		enabled = b;
		if(enabled){
			load();
			resetTimer();
		}else{
			mm=null;
		}
	}
	
	public FigureIterator getFigures(Rectangle area) {
		if(enabled)
			return super.getFigures(area);
		return null;
	}

}
